# ================ CONFIG
options = [
    "p"  # tmp file path
    "s" # stress
]
optStr = ":".join(options) + ":"
USER_OUTPUT_FILE = "out.txt"
STD_OUTPUT_FILE = "std.txt"
INPUT_FILE = "./in.txt"
TMP_OUT = "./tmp.out"
JUDGE_PATH = "./judge"

config = dict()
config["p"] = "./judge"
config["s"] = False

CPP_ARGS = [
    "-std=c++14",
    "-O2",
    "-fsanitize=address",
]
CPP_EX = "g++ {} -o {} " + " ".join(CPP_ARGS)

PYTHON_ARGS = [

]
PYTHON_EX = "python {} " + " ".join(PYTHON_ARGS)

JAVA_ARGS = [

]
JAVA_EX = "javac {}"

MAP_EXT_CMD = {
    "cpp" : CPP_EX,
    "java" : JAVA_EX,
    "py" : PYTHON_EX
}

# ================ CONFIG

import getopt, sys, os, re

class Checker:
    def compare(a, b):
        raise Exception("not impl")

class DigitsChecker(Checker):

    def compare(self, a, b):
        fa = open(a.output, "r")
        contenta = fa.read()
        fa.close()
        fb = open(b.output, "r")
        contentb = fb.read()
        fb.close()
        da = re.split('[ \n]', contenta)
        la = []
        for s in da:
            if s.__len__() > 0:
                la.append(int(s))
        # print(la)

        db = re.split('[ \n]', contentb)
        lb = []
        for s in db:
            if s.__len__() > 0:
                lb.append(int(s))
        # print(lb)

        if la.__len__() != lb.__len__():
            return "WA: different number of digits"
        
        n = la.__len__()
        for i in range(n):
            if la[i] != lb[i]:
                return "WA: diff on No.{} number: {} & {}".format(i+1, da[i], db[i])
        
        return "AC"

class TokensChecker(Checker):
    def compare(self, a, b):
        fa = open(a.output, "r")
        contenta = fa.read()
        fa.close()
        fb = open(b.output, "r")
        contentb = fb.read()
        fb.close()
        da = re.split('[ \n]', contenta)
        la = []
        for s in da:
            if s != '':
                la.append(s)
        # print(la)

        db = re.split('[ \n]', contentb)
        lb = []
        for s in db:
            if s != '':
                lb.append(s)
        # print(lb)

        if la.__len__() != lb.__len__():
            return "WA: different number of digits"
        
        n = la.__len__()
        for i in range(n):
            if la[i] != lb[i]:
                return "WA: diff on No.{} token: {} & {}".format(i+1, da[i], db[i])
        
        return "AC"
class Runner:
    CPP_EX = "g++ {} -o {}"
    def __init__(self, source, target = None, output = None):
        self.source = source
        self.target = target
        self.output = output
    def compile(self):
        # raise Exception("not impl")
        self.ext = self.source.split("/")[-1].split(".")[-1]
        command = MAP_EXT_CMD[self.ext].format(self.source, self.target)
        print("log: compiling {} file, executing '{}'".format(self.ext, command))
        os.system(command)
        print("log: compiling completed")
    def run(self):
        os.system("{} < {} > {}".format(self.target, INPUT_FILE, self.output))
        print("log: running completed")


